﻿using Dapper_Core.Common;
using Dapper_Core.DAL.Impl;
using Dapper_Core.Model;
using Dapper_Core.Table;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Dapper_Core.BLL.Impl
{
    public class V_WorkflowTransitionBusiness : BaseRepository<V_WorkflowTransition>
    {
        public V_WorkflowTransitionBusiness()
        {

        }

        public V_WorkflowTransitionBusiness(TransactionContext transactionContext) : base(transactionContext)
        {

        }

        public V_WorkflowTransition GetStartTransition(string workflowCode)
        {
            try
            {
                if (string.IsNullOrEmpty(workflowCode)) return null;
                Expression<Func<V_WorkflowTransition, bool>> exp = transaction => transaction.WorkflowCode == workflowCode && transaction.FromNodeType == "start";
                List<V_WorkflowTransition> list = GetList(exp).ToList();
                if (list != null && list.Count > 0) return list[0];
                return null;
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "获取流程的首步Transition失败", "workflowCode=" + workflowCode, ex, "DapperCore");
                return null;
            }
        }

        public List<V_WorkflowTransition> GetNextTransition(string workflowCode, string fromCode, string action)
        {
            try
            {
                if (string.IsNullOrEmpty(workflowCode) || string.IsNullOrEmpty(fromCode)) return new List<V_WorkflowTransition>();
                Expression<Func<V_WorkflowTransition, bool>> exp = transaction => transaction.WorkflowCode == workflowCode && transaction.FromCode == fromCode && transaction.Code == action;
                List<V_WorkflowTransition> list = GetList(exp).ToList();
                if (list != null && list.Count > 0) return list;
                return new List<V_WorkflowTransition>();
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "获取流程的下一步Transition失败", $"workflowCode={ workflowCode}，fromCode={fromCode}，action={action}", ex, "DapperCore");
                return new List<V_WorkflowTransition>();
            }
        }
    }
}
